//
//  LBMediaPresentViewController.m
//  LangBa
//
//  Created by Mac on 2017/12/21.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BBShareNewsPresentViewController.h"
#import "UIImage+STTools.h"
@interface BBShareNewsPresentViewController ()
@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIView                     *customView;
@property(nonatomic, strong) UIImage                     *image;

@end

@implementation BBShareNewsPresentViewController

- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                       backgroundImage:nil
                                                 image:nil];
    [weakSelf.clearButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    [self.view addSubview:_clearButton];
    
    self.customView = [[UIView alloc] initWithFrame:CGRectMake(0, UIScreenHeight, UIScreenWidth, 342)];
    self.customView.centerX = UIScreenWidth / 2;
    self.customView.top = UIScreenHeight;
    self.customView.backgroundColor = [UIColor whiteColor];
    [self.clearButton addSubview:self.customView];
    UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectedWhiteView)];
    self.customView.userInteractionEnabled = YES;
    [self.customView addGestureRecognizer:ges];
    
    BBUserModel * user = [STUserManger defult].loginedUser;
    //sub
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 40)
                                                     text:@"此二维码已包含邀请\"邀请下级\"等功能\n如未登录，将不能获得奖励"
                                                textColor:FirstTextColor
                                                     font:10
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [self.customView addSubview:titleLable];
    
    
    NSArray * titles = @[@"微信",@"朋友圈",@"QQ",@"QQ空间"];
    NSArray * images = @[@"微信2",@"朋友圈",@"icon_QQ2",@"qqzone-1"];
    CGFloat menuWith = UIScreenWidth / titles.count;
    for (NSInteger i = 0; i < titles.count; i ++) {
        NSString * title = titles[i];
        NSString * imageName = images[i];
        STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 20, menuWith, menuWith)
                                                         title:title
                                                    titleColor:nil
                                                     titleFont:0
                                                  cornerRadius:0
                                               backgroundColor:nil
                                               backgroundImage:nil
                                                         image:[UIImage imageNamed:imageName]];
        buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self.customView addSubview:buyButton];
        [buyButton setClicAction:^(UIButton *sender) {
            [weakSelf dismissWithHandle:^{
                if (weakSelf.shareButtonHandle) {
                    weakSelf.shareButtonHandle(sender.currentTitle,weakSelf.image);
                }
                // [SVProgressHUD showInfoWithStatus:[NSString stringWithFormat:@"分享到%@账号正在申请",title]];
            }];
        }];
        buyButton.left = i * menuWith;
        
    }
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, menuWith + 20  + 1  , UIScreenWidth, 1)];
    line.backgroundColor = TM_lineColor;
    [self.customView addSubview:line];
    UIColor * titleClolr = FlatOrange;
    if (BBThemeColorManger.isBlack) {
        titleClolr = UIColor.whiteColor;
        self.customView.backgroundColor = BBThemeColorManger.blackStyletableGrayBackColor;
    }
    STButton * cancelButton = [[STButton alloc] initWithFrame:CGRectMake(0, line.bottom, UIScreenWidth, 44)
                                                        title:@"取消"
                                                   titleColor:titleClolr
                                                    titleFont:14
                                                 cornerRadius:0
                                              backgroundColor:nil
                                              backgroundImage:nil
                                                        image:nil];
    cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [cancelButton setClicAction:^(UIButton *sender) {
        [weakSelf dismissWithHandle:^{
            
        }];
    }];
    [self.customView addSubview:cancelButton];
    
    
    
    
    
    self.customView.height = cancelButton.bottom;
    self.customView.top = UIScreenHeight;
    
    CGFloat maxHeight = UIScreenHeight - self.customView.height - 60;
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth - 30, 200)];
    imageView.centerX = UIScreenWidth / 2;
    imageView.bottom = - 20;
    imageView.layer.cornerRadius = 10;
    imageView.clipsToBounds = YES;
    imageView.backgroundColor = [UIColor whiteColor];
    [self.customView addSubview:imageView];
    
    [self configImageView:imageView];
    
}
- (void)configImageView:(UIImageView*)imageView{
    
    BBUserModel * user = [STUserManger defult].loginedUser;
    
    UIImageView * topimageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imageView.width, imageView.width / 2)];
    topimageView.image = [UIImage imageNamed:@"全球首家可挖矿区块链媒体"];
    if (user) {
        [topimageView sd_setImageWithURL:[NSURL URLWithString:user.share_news_bg] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            self.image = [UIImage st_snapshot:imageView];
        }];
    }

    [imageView addSubview:topimageView];
    
    
    STButton * timeButton = [[STButton alloc] initWithFrame:CGRectMake(20, topimageView.bottom + 20, 200, 17)
                                                title:self.time
                                           titleColor:SecendTextColor
                                            titleFont:14
                                         cornerRadius:0
                                      backgroundColor:nil
                                      backgroundImage:nil
                                                image:[UIImage imageNamed:@"沙漏"]];
    
    timeButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    
    [imageView addSubview:timeButton];
    
    CGFloat font = [BBSystemManger manger].share_font_size.floatValue;
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(timeButton.left, timeButton.bottom + 10, imageView.width - 2 *timeButton.left , 30)
                                                     text:self.news
                                                textColor:SecendTextColor
                                                     font:font
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [NSString lableAutoAdjustheightWithLabel:titleLable];
    
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:self.news];
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.lineSpacing = 5;
    NSMutableAttributedString * finsh =   [[NSMutableAttributedString alloc] initWithAttributedString:attrStr];
    [finsh addAttributes:@{NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:@0.1f
                           } range:NSMakeRange(0, finsh.length)];
    [finsh addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} range:NSMakeRange(0, finsh.length)];
    
    titleLable.attributedText = finsh;
     CGFloat   allheight = [self getSpaceLabelHeight:titleLable.attributedText.string withFont:[UIFont systemFontOfSize:font] withWidth:titleLable.width];
    titleLable.height = allheight;
    
    [imageView addSubview:titleLable];
    
    
    UIImageView * codeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(timeButton.left, titleLable.bottom + 70, 80, 80)];
    if (user) {
        codeImageView.image = [UIImage st_imageWithDefaultTwoCode:user.share];
    }else{
        NSString * name =  [BBSystemManger manger].share;
        codeImageView.image = [UIImage st_imageWithDefaultTwoCode:name];
    }
    [imageView addSubview:codeImageView];
    
    
    STLabel * desLable = [[STLabel alloc] initWithFrame:CGRectMake(codeImageView.right + 20, codeImageView.top, imageView.width, 20)
                                                   text:@"扫码领取"
                                              textColor:SecendTextColor
                                                   font:14
                                            isSizetoFit:NO
                                          textAlignment:NSTextAlignmentLeft];
    [imageView addSubview:desLable];
    
    UIColor * red = [UIColor colorWithRed:247/255.0 green:0/255.0 blue:137.0/255.0 alpha:1];
    STLabel * tmoneyLable = [[STLabel alloc] initWithFrame:CGRectMake(desLable.left, 200, imageView.width, 40)
                                                      text:[NSString stringWithFormat:@"%@ %@糖果🍬"
                                                            
                                                            ,[BBSystemManger manger].registerAward,[BBSystemManger manger].candyName]
                                                 textColor:red
                                                      font:28
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentLeft];
    tmoneyLable.top = desLable.bottom  + 10;
    NSString * name = [NSString stringWithFormat:@"%@糖果",[BBSystemManger manger].candyName];
    tmoneyLable.attributedText = [tmoneyLable.text st_convertAttributeStringWithKeyWord:name
                                                                             attributes:@{NSForegroundColorAttributeName:SecendTextColor,NSFontAttributeName:[UIFont systemFontOfSize:18]}];
    [imageView addSubview:tmoneyLable];
    
    
    STLabel * longLable = [[STLabel alloc] initWithFrame:CGRectMake(codeImageView.right  +20, tmoneyLable.bottom, 100, 20)
                                                    text:@"长按识别二维码"
                                               textColor:SecendTextColor
                                                    font:12
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentLeft];
    longLable.top = tmoneyLable.bottom ;
    [imageView addSubview:longLable];
    
    imageView.height = longLable.bottom  +30;
    imageView.bottom =  - 20;
    
    CGFloat maxHeight = UIScreenHeight - self.customView.height - 40;
    
    if (imageView.height > maxHeight) {
        CGFloat scale = maxHeight/imageView.height;
        imageView.transform = CGAffineTransformMakeScale(scale, scale);
        imageView.bottom =  - 20;
        imageView.centerX = UIScreenWidth / 2;
    }
    self.image = [UIImage st_snapshot:imageView];
    
}
#pragma mark --Private Method
- (void)show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.customView.bottom = UIScreenHeight;
                     } completion:^(BOOL finished) {
                         
                     }];
}
- (void)dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
- (void)dismissWithHandle:(void(^)())handle{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (handle) {
                handle();
            }
        }];
    }];
}
#pragma mark --Action Method

- (void)onSelectedCancleButton{
    
    [self dismiss];
    
}
- (void)onSelectedWhiteView{
    [self.view endEditing:YES];
}

#pragma mark --NetWork Method
- (void)sendQrcodeRequest{
    
}
-(CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width {
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    
    paraStyle.alignment = NSTextAlignmentLeft;
    
    paraStyle.lineSpacing = 5;
    
    
    paraStyle.firstLineHeadIndent = 0.0;
    
    paraStyle.paragraphSpacingBefore = 0.0;
    
    paraStyle.headIndent = 0;
    
    paraStyle.tailIndent = 0;
    
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
                          };
    
    
    
    CGSize size = [str boundingRectWithSize:CGSizeMake(width, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
    return size.height;
    
}
@end





